﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eService.BLL;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;

namespace eService.Presentation
{
    public partial class usrcDanhMuc_NhanVien : UserControl
    {
        EnumNameForm.ThaoTac thaotac;
        ExcuteNHANVIEN exe;
        ExcuteNHOMNHANVIEN exeNhom;

        public usrcDanhMuc_NhanVien()
        {
            InitializeComponent();
            LoadAll();
        }
        protected virtual void LoadAll()
        {
            LoadConnection();
            LoadUI();
            LoadData();
        }
        private void LoadConnection()
        {
            ConnectionProvider.LoadAllConnection();
        }
        private void LoadUI()
        {
            exe = new ExcuteNHANVIEN();
            exeNhom = new ExcuteNHOMNHANVIEN();
            thaotac = EnumNameForm.ThaoTac.macdinh;
            ControlUI(thaotac);
            UIHelper.LoadFocus(txtTimKiem);
        }
        public void LoadData()
        {
            LoadCmbNhom();
        }
        private void LoadCmbNhom()
        {
            try
            {
                List<GROUPEMPLOYEE> list = exeNhom.GetAll();
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("Nhóm", typeof(string));
                foreach (GROUPEMPLOYEE obj in list)
                {
                    data.Rows.Add(obj.GeID, obj.GeName);
                }
                cmbNhom.DataSource = data;
                cmbNhom.DisplayMember = "Nhóm";
                cmbNhom.ValueMember = "Mã";
                cmbNhom.ColumnWidths = "0,200";
                cmbNhom.SelectedIndex = -1;
                cmbNhom.Text = "";
            }
            catch
            {

            }
        }
        private void ControlUI(EnumNameForm.ThaoTac thaotac)
        {
            if (thaotac == EnumNameForm.ThaoTac.them || thaotac == EnumNameForm.ThaoTac.sua)
            {
               
                if (thaotac == EnumNameForm.ThaoTac.them)
                {
                    LoadValuesToUI(null);
                    Int32 Stt = 1;
                    List<EMPLOYEE> list = exe.GetAll();
                    if (list != null) Stt = list.Count + 1;
                    txtMa.Text = "NV" + Stt.ToString("D4");
                    labSTT.Text = Stt.ToString();
                }
                if (thaotac == EnumNameForm.ThaoTac.sua && txtMa.Text == "")
                {
                    MessageBox.Show("Vui lòng chọn nhân viên!");
                    return;
                }
                SetStateButton(false);
                SetTextReadOnly(false);
                UIHelper.LoadFocus(txtTen);
            }
            if (thaotac == EnumNameForm.ThaoTac.macdinh || thaotac == EnumNameForm.ThaoTac.xoa)
            {
                SetStateButton(true);
                LoadValuesToUI(null);
                SetTextReadOnly(true);

                //Load TreeView
                FillTreeView();
                treeview.RefreshGrouping();

                txtTimKiem.Text = "";
                UIHelper.LoadFocus(txtTimKiem);
            }
        }
        private void SetStateButton(bool IsEnabled)
        {
            btnSua.Enabled = IsEnabled;
            btnThem.Enabled = IsEnabled;
            btnXoa.Enabled = IsEnabled;
            btnXacNhan.Visible = !IsEnabled;
            btnHuy.Visible = !IsEnabled;
            treeview.Enabled = IsEnabled;
            btnTimKiem.Enabled = IsEnabled;
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtTen.ReadOnly = IsReadOnly;
            txtGhiChu.ReadOnly = IsReadOnly;
            dtNgaySinh.Enabled = !IsReadOnly;
            txtCMND.ReadOnly = IsReadOnly;
            txtDiaChiTamTru.ReadOnly = IsReadOnly;
            txtDiaChiThuongTru.ReadOnly = IsReadOnly;
            txtDienThoai.ReadOnly = IsReadOnly;
            txtEmail.ReadOnly = IsReadOnly;
            cmbNhom.Enabled = !IsReadOnly;
        }

        private void LoadValuesToUI(EMPLOYEE obj)
        {
            if (obj == null)
            {
                obj = new EMPLOYEE();
                obj.EmpAddress = "";
                obj.EmpCount = 0;
                obj.EmpDate = DateTime.Today.AddYears(-20);
                obj.EmpEmail = "";
                obj.EmpID = "";
                obj.EmpIdCard = "";
                obj.EmpName = "";
                obj.EmpNotes = "";
                obj.EmpPhone = "";
                obj.EmpResident = "";
                obj.EmpStatus = 1;
                obj.GeID = "";
            }

            txtMa.Text = obj.EmpID;
            txtTen.Text = obj.EmpName;
            dtNgaySinh.Value = (DateTime)obj.EmpDate;
            txtCMND.Text = obj.EmpIdCard;
            txtDiaChiThuongTru.Text = obj.EmpAddress;
            txtDiaChiTamTru.Text = obj.EmpResident;
            txtEmail.Text = obj.EmpEmail;
            txtDienThoai.Text = obj.EmpPhone;
            txtGhiChu.Text = obj.EmpNotes;
            labMaNhom.Text = obj.GeID;
            labSTT.Text = obj.EmpCount.ToString();

            GROUPEMPLOYEE objGrp = exeNhom.GetByID((string)obj.GeID);
            if (objGrp != null) cmbNhom.Text = objGrp.GeName;
            else cmbNhom.Text = "";

        
        }
        private void FillTreeView()
        {
            List<EMPLOYEE> data = exe.GetAll();
            exe.LoadToTreeview(treeview, data);
        }
        private bool XuLy(EnumNameForm.ThaoTac thaotac)
        {
            var flag = false;
            try
            {
                EMPLOYEE obj = exe.LoadValues(txtMa.Text,txtTen.Text,dtNgaySinh.Value,labMaNhom.Text,txtDiaChiThuongTru.Text,txtDiaChiTamTru.Text,txtCMND.Text,txtEmail.Text,txtDienThoai.Text,1,txtGhiChu.Text,int.Parse(labSTT.Text));
                flag = exe.KQ;
                if (flag)
                {
                    flag = false;
                    switch (thaotac)
                    {
                        case EnumNameForm.ThaoTac.them:
                            flag = exe.Add(obj);
                            break;
                        case EnumNameForm.ThaoTac.sua:
                            flag = exe.UpDate(obj);
                            break;
                        case EnumNameForm.ThaoTac.xoa:
                            flag = exe.Delete(obj);
                            break;
                    }
                    flag = exe.KQ;
                }
                MessageBox.Show(exe.ErrMsg, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        private void button_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();

            if ((_thaotac == "sua" && txtMa.Text == "") || (_thaotac == "xoa" && txtMa.Text == ""))
            {
                MessageBox.Show("Vui lòng chọn nhân viên!");
                return;
            }

            switch (_thaotac)
            {
                case "them":
                    thaotac = EnumNameForm.ThaoTac.them;
                    break;
                case "sua":
                    thaotac = EnumNameForm.ThaoTac.sua;
                    break;
                case "huy":
                    thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xoa":
                    KQ = XuLy(EnumNameForm.ThaoTac.xoa);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xacnhan":
                    KQ = XuLy(thaotac);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
            }
            ControlUI(thaotac);
        }

        private void treeview_AfterSelectionChange(object sender, HLVControl.Grid.Events.TreeListElementEventArgs e)
        {
            try
            {
                TreeListRow firstSelected = treeview.SelectedElements[0] as TreeListRow;
                string ID = firstSelected.Cells["colID"].Value.ToString();
                EMPLOYEE obj = exe.GetByID(ID);
                if (obj == null) return;
                LoadValuesToUI(obj);
                txtTen.Focus();
            }
            catch
            {

            }
        }
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> { });
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtMa.Name)
                    UIHelper.txt_KeyPress(sender, txtTen, e);
                if (txt.Name == txtTen.Name)
                    UIHelper.txt_KeyPress(sender, dtNgaySinh, e);
                if (txt.Name == dtNgaySinh.Name)
                    UIHelper.txt_KeyPress(sender, txtCMND, e);
                if (txt.Name == txtCMND.Name)
                    UIHelper.txt_KeyPress(sender, txtDiaChiThuongTru, e);
                if (txt.Name == txtDiaChiThuongTru.Name)
                    UIHelper.txt_KeyPress(sender, txtDiaChiTamTru, e);
                if (txt.Name == txtDiaChiTamTru.Name)
                    UIHelper.txt_KeyPress(sender, txtEmail, e);
                if (txt.Name == txtEmail.Name)
                    UIHelper.txt_KeyPress(sender, cmbNhom, e);
                if (txt.Name == cmbNhom.Name)
                    UIHelper.txt_KeyPress(sender, txtDienThoai, e);
                if (txt.Name == txtDienThoai.Name)
                    UIHelper.txt_KeyPress(sender, txtGhiChu, e);
                if (txt.Name == txtGhiChu.Name)
                    UIHelper.txt_KeyPress(sender, btnXacNhan, e);
                if (txt.Name == btnXacNhan.Name)
                    UIHelper.txt_KeyPress(sender, txtTimKiem, e);
                if (txt.Name == txtTimKiem.Name)
                    UIHelper.txt_KeyPress(sender, btnTimKiem, e);
                if (txt.Name == btnTimKiem.Name)
                    UIHelper.txt_KeyPress(sender, treeview, e);
            }
            catch { }
        }

        private void cmbNhom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNhom.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbNhom.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labMaNhom.Text = Ma;
                cmbNhom.Text = Ten;
            }
        }
        
    }
}
